﻿
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;



namespace FXnaEngine.Graphics
{

    /// <summary>
    /// 用于定义基本的材质信息
    /// </summary>
    public class FMaterial
    {

        #region Member Fields
        protected float specularPower;

        float alpha;


        protected Vector4 specularColor;
        protected Vector4 ambientColor;
        protected Vector4 emissiveColor;
        protected Vector4 diffuseColor;

        protected Texture2D texture;
        protected Effect internalEffect;

        protected bool hasChanged;




        /// <summary>
        /// Diffuse texture for the material. Can be null for unused.
        /// </summary>
        public Texture diffuseTexture = null;
        /// <summary>
        /// Normal texture in case we use normal mapping. Can be null for unused.
        /// </summary>
        public Texture normalTexture = null;
        /// <summary>
        /// Height texture in case we use parallax mapping. Can be null for unused.
        /// </summary>
        public Texture heightTexture = null;
        /// <summary>
        /// Detail texture, used for landscape rendering. Can be null for unused.
        /// </summary>
        public Texture detailTexture = null;

        /// <summary>
        /// Parallax amount for parallax and offset shaders.
        /// </summary>
        public float parallaxAmount =  0.04f;



        #endregion

        #region Constructors
        /// <summary>
        /// Creates a default material. See each properties for default values.
        /// </summary>
        public FMaterial()
        {

            alpha = 1;

            specularPower = 10.0f;

            specularColor = Vector4.Zero;
            ambientColor = Vector4.One;
            emissiveColor = Vector4.Zero;
            //diffuseColor = new Vector4(0.1f, 0.1f, 0.1f, 1);
            diffuseColor = Vector4.One;

            texture = null;
            internalEffect = null;
            hasChanged = false;

        }
        #endregion

        #region Properties




        public float Alpha
        {
            get { return alpha; }
            set { alpha = value; }
        }

        /// <summary>
        /// Gets or sets the shininess of this material when highlighted with lights.
        /// The larger the specular power, the smaller the size of the specular highlight.
        /// The default value is 10.0f
        /// </summary>
        public virtual float SpecularPower
        {
            get { return specularPower; }
            set
            {
                if (specularPower != value)
                {
                    specularPower = value;
                    hasChanged = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the diffuse color of this material.
        /// The default value is Color.Black
        /// </summary>
        public virtual Vector4 Diffuse
        {
            get { return diffuseColor; }
            set
            {
                if (!diffuseColor.Equals(value))
                {
                    diffuseColor = value;
                    hasChanged = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the ambient color of this material.
        /// The default value is Color.Black
        /// </summary>
        public virtual Vector4 Ambient
        {
            get { return ambientColor; }
            set
            {
                if (!ambientColor.Equals(value))
                {
                    ambientColor = value;
                    hasChanged = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the specular color of this material.
        /// The default value is Color.Black
        /// </summary>
        public virtual Vector4 Specular
        {
            get { return specularColor; }
            set
            {
                if (!specularColor.Equals(value))
                {
                    specularColor = value;
                    hasChanged = true;
                }
            }
        }

        /// <summary>
        /// Gets or sets the color of the light this material emits. 
        /// The default value is Color.Black
        /// </summary>
        public virtual Vector4 Emissive
        {
            get { return emissiveColor; }
            set
            {
                if (!emissiveColor.Equals(value))
                {
                    emissiveColor = value;
                    hasChanged = true;
                }
            }
        }

        /// <summary>
        /// Gets whether this material contains texture information.
        /// </summary>
        public virtual bool HasTexture
        {
            get { return (texture != null); }
        }

        /// <summary>
        /// Gets or sets the texture applied to this material.
        /// </summary>
        public virtual Texture2D Texture
        {
            get { return texture; }
            set
            {
                texture = value;
                hasChanged = true;
            }
        }

        /// <summary>
        /// Gets or sets whether there is a change in the material setting
        /// </summary>
        /// <remarks>
        /// Do not set this value in your application. It is set by the system.
        /// </remarks>
        public virtual bool HasChanged
        {
            get { return hasChanged; }
            set { hasChanged = value; }
        }

        /// <summary>
        /// Gets or sets the effect associated with model contents. Some model files include
        /// their own material information.
        /// </summary>
        /// <remarks>
        /// See XNA's reference manual for the details of an "Effect" class
        /// </remarks>
        public virtual Effect InternalEffect
        {
            get { return internalEffect; }
            set { internalEffect = value; }
        }
        #endregion

        #region Public Methods

        public virtual void Dispose()
        {
            if (texture != null)
                texture.Dispose();
        }

        #endregion



    }




}
